﻿using Newtonsoft.Json.Linq;
using StockUI.Entity;

namespace StockUI;


public  class IPHelper
{
    
    public static string GetIP()
    {
        var result = GradIPCN();
        if (result != "Fail")
        {
            return result;
        }
        result = GetIPCSDN();
        if (result != "Fail")
        {
            return result;
        }
        return "请检查网络";
    }

    #region IP
    /// <summary>
    /// https://webapi-pc.meitu.com/common/ip_location
    /// </summary>
    /// <returns></returns>
    private static string GradIPCN()
    {
        int year =DateTime.Now.Year;
        int month =DateTime.Now.Month;
        int day =DateTime.Now.Day;
        DateTime startDate = new DateTime(year, month, day);
        var loginlog= new SQLiteHelper<LoginLog>().QuerySingle(a =>  a.UpdateDate > startDate);
        if (loginlog==null)
        {
            string url = "https://webapi-pc.meitu.com/common/ip_location";
            HttpClientHelper httpClientHelper = new HttpClientHelper();
            var ipcn = httpClientHelper.Get<WebApiPC>(null, url);
            if (ipcn.code == 0)
            {
                //解析字符串数据
                JObject obj = JObject.Parse(ipcn.data.ToString());

                var key = string.Empty;
                foreach (var item in obj)
                {
                    key = item.Key;
                    break;
                }
                var jsondata = JObject.Parse(obj[key].ToString());
                new SQLiteHelper<LoginLog>().Insert(new LoginLog {
                 IP= key,
                 Nation= jsondata["nation"].ToString(),
                 Province = jsondata["province"].ToString(),
                 City = jsondata["city"].ToString(),
                 Isp= jsondata["isp"].ToString(),
                 UpdateDate =DateTime.Now
                });
                return $"{key} {jsondata["nation"]} {jsondata["province"]} {jsondata["city"]} {jsondata["isp"]}";
            }
        }
        else
        {
            return $"{loginlog.IP} {loginlog.Nation} {loginlog.Province} {loginlog.City} {loginlog.Isp}";
        }
       
        
        return "Fail";
    }
    class WebApiPC
    {
        public string reqid { get; set; }
        public int code { get; set; }
        public object data { get; set; }
    }
  
private  static string GetIPCSDN()
    {
        //API地址：https://searchplugin.csdn.net/api/v1/ip/get?ip=[IP地址串，不填时为查询本机客户端IP]
        string url = "https://searchplugin.csdn.net/api/v1/ip/get";
        HttpClientHelper httpClientHelper = new HttpClientHelper();
        var ipcn = httpClientHelper.Get<IPCSDN>(null, url);
        if (ipcn.code == 200)
        {
            return $"{ipcn.data.ip}  {ipcn.data.address}";
        }
        return "Fail";
    }
    class IPCSDN
    {
        public string msg { get; set; }
        public int code { get; set; }
        public IPCSDNItem data { get; set; }
    }
    class IPCSDNItem
    {
        public string address { get; set; }
        public string ip { get; set; }
    }
        #endregion
    }
